package cc.io24.knewcafe.blog.facade;


import cc.io24.knewcafe.blog.facade.modle.GuShiCi;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class GuShiCiService {

    OkHttpClient client = new OkHttpClient();

    public GuShiCi getShiCi(){
        String content = "";
        Request request = new Request.Builder()
                .url("http://api.gushi.ci/rensheng/lizhi")
                .build();

        try {
            Response response = client.newCall(request).execute();
            content = response.body().string();
            System.out.println(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.readValue(content,GuShiCi.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    

    public List<GuShiCi> getList(int size){
        List<GuShiCi> guShiCis = new ArrayList<>();
        for(int i=0;i<size;i++){
            guShiCis.add(this.getShiCi());
        }
        return guShiCis;
    }

}
